package com.example.cr.insect.service;

import cn.hutool.core.bean.BeanUtil;
import com.example.cr.common.response.PageResponse;
import com.example.cr.insect.entity.Location;
import com.example.cr.insect.entity.LocationExample;
import com.example.cr.insect.mapper.LocationMapper;
import com.example.cr.insect.request.LocationListRequest;
import com.example.cr.insect.response.LocationResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
    import cn.hutool.core.date.DateTime;
    import cn.hutool.core.util.ObjectUtil;
    import com.example.cr.common.util.SnowflakeUtil;
    import com.example.cr.insect.request.LocationRequest;

@Service
public class LocationService {
private static final Logger log = LoggerFactory.getLogger(LocationService.class);
@Autowired
LocationMapper locationMapper;

public PageResponse<LocationResponse> list(LocationListRequest request) {
    LocationExample locationExample = new LocationExample();
    locationExample.setOrderByClause("id desc");

    // 处理搜索条件
    LocationExample.Criteria criteria = locationExample.createCriteria();
    if (request.getKeyword() != null && !request.getKeyword().isEmpty()) {
    String keyword = "%" + request.getKeyword() + "%";
                criteria.andNameLike(keyword);
    }

    PageHelper.startPage(request.getPage(), request.getSize());
    log.info("当前页码 current_page = {}", request.getPage());
    log.info("每页记录数 per_page = {}", request.getSize());

    List<Location> locations = locationMapper.selectByExample(locationExample);

    PageInfo<Location> pageInfo = new PageInfo<>(locations);
    log.info("总记录数 total = {}", pageInfo.getTotal());
    log.info("总页数 pages = {}", pageInfo.getPages());

    List<LocationResponse> list = BeanUtil.copyToList(locations, LocationResponse.class);

        PageResponse<LocationResponse> pageResponse = new PageResponse<>();
            pageResponse.setTotal(pageInfo.getTotal());
            pageResponse.setList(list);

            return pageResponse;
            }
            public void save(LocationRequest request) {
            Location location = BeanUtil.copyProperties(request, Location.class);
            DateTime now = DateTime.now();
            if (ObjectUtil.isNull(location.getId()) || location.getId() == 0L) {
            location.setId(SnowflakeUtil.getId());
            location.setCreatedAt(now);
            location.setUpdatedAt(now);
            locationMapper.insert(location);
            } else {
            location.setUpdatedAt(now);
            locationMapper.updateByPrimaryKeySelective(location);
            }
            }

            public int deleteBatch(List<Long> ids) {
                LocationExample locationExample = new LocationExample();
                LocationExample.Criteria criteria = locationExample.createCriteria();
                criteria.andIdIn(ids);
                return locationMapper.deleteByExample(locationExample);
                }
                }
